﻿Public Class USERPOLICYEDIT
    Dim connect As CONDB = CONDB.NewConnection()
    Dim publicEmpid As String

    Public Sub New(ByRef empid As String)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        publicEmpid = empid
        empIDLa.Text = empid

        Dim empName As String = connect.GetTable("SELECT CONCAT_WS(' ', NAME, LNAME) as 'ชื่อ' FROM hospemp WHERE EMPID = '" & empid & "'").Rows(0)(0)
        empNameLa.Text = empName
    End Sub

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub permissionType_ValueChanged(sender As Object, e As EventArgs) Handles permissionTypeSwitch.ValueChanged
        If permissionTypeSwitch.Value = True Then
            filterSoftware()
            PermissionSetPanel.Visible = True
            grantGroupPermission.Visible = False
        Else
            filterGroupPolicy()
            PermissionSetPanel.Visible = False
            grantGroupPermission.Visible = True
        End If
    End Sub

    Public Sub queryUserPolicie()
        Dim userPolicy As New DataTable
        userPolicy = connect.GetTable("SELECT a.`UPID` AS 'UPID', a.`EMPID` AS 'รหัสพนักงาน', a.`PRGID` AS 'รหัสซอฟต์แวร์', b.`PRGCODE` as 'Code ซอฟต์แวร์', b.`PRGNAME` AS 'ชื่อซอฟต์แวร์', a.`PADD` AS 'ADD', a.`PDEL` AS 'DEL', a.`PUPD` AS 'UPDATE', a.`PPRT` AS 'PRINT', a.`PSEE` AS 'VIEW', a.`STATUS` AS 'สถานะการเปิดใช้งาน' FROM usrpolicy a LEFT JOIN masprg b ON a.PRGID = b.PRGID WHERE EMPID = '" & publicEmpid & "'")
        policyGrid.PrimaryGrid.DataSource = userPolicy
    End Sub

    Private Sub filterSoftware()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(PRGID AS CHAR(4)) AS PRGID, PRGNAME, PRGCODE FROM masprg WHERE PRGID NOT IN (SELECT PRGID FROM usrpolicy WHERE EMPID = '" & publicEmpid & "');"
        s = New FILTERCLASS(prgListTxt, sql, "id software,ชื่อ software,โค้ด software", "100,140,100", "1,1,1", "1,1,1")
        s.SetShowInDown = True
        s.SetShowBorder = True
    End Sub

    Private Sub filterGroupPolicy()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(GPRGID AS CHAR(6)) AS GPRGID, GPRGNAME FROM masgrpprg WHERE `STATUS` = '1';"
        s = New FILTERCLASS(prgListTxt, sql, "id กลุ่ม software,ชื่อกลุ่ม software", "100,140", "1,1", "1,1")
        s.SetShowInDown = True
        s.SetShowBorder = True
    End Sub

    Private Sub prgListTxt_Click(sender As Object, e As EventArgs) Handles prgListTxt.Click
        prgListTxt.Clear()
    End Sub

    Private Sub clearAddPolicieForm()
        prgListTxt.Clear()
        addChk.Checked = False
        delChk.Checked = False
        updateChk.Checked = False
        printChk.Checked = False
        viewChk.Checked = False
        statusChk.Checked = False
    End Sub

    Private Sub USERPOLICYEDIT_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        queryUserPolicie()
        filterGroupPolicy()
        PermissionSetPanel.Visible = False
        grantGroupPermission.Visible = True
    End Sub

    Private Sub policyGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles policyGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow

        If e.GridCell.ColumnIndex = 5 Or e.GridCell.ColumnIndex = 6 Or e.GridCell.ColumnIndex = 7 Or e.GridCell.ColumnIndex = 8 Or e.GridCell.ColumnIndex = 9 Or e.GridCell.ColumnIndex = 10 Then
            Dim editStat As Integer
            If e.GridCell.ColumnIndex = 5 Then
                editStat = MsgBox("ท่านต้องการแก้ไขสิทธิ์การเพิ่มข้อมูลซอฟต์แวร์ดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
            ElseIf e.GridCell.ColumnIndex = 6 Then
                editStat = MsgBox("ท่านต้องการแก้ไขสิทธิ์การลบข้อมูลซอฟต์แวร์ดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
            ElseIf e.GridCell.ColumnIndex = 7 Then
                editStat = MsgBox("ท่านต้องการแก้ไขสิทธิ์การแก้ไขข้อมูลซอฟต์แวร์ดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
            ElseIf e.GridCell.ColumnIndex = 8 Then
                editStat = MsgBox("ท่านต้องการแก้ไขสิทธิ์การพิมพ์ข้อมูลซอฟต์แวร์ดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
            ElseIf e.GridCell.ColumnIndex = 9 Then
                editStat = MsgBox("ท่านต้องการแก้ไขสิทธิ์การดูข้อมูลซอฟต์แวร์ดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
            ElseIf e.GridCell.ColumnIndex = 10 Then
                editStat = MsgBox("ท่านต้องการแก้ไขสถานะการเปิดใช้งานซอฟต์แวร์ดังกล่าวใช่หรือไม่ (หากคลิกใช่จะเป็นการถอดถอนสิทธิ์การใช้งานซอฟต์แวร์ดังกล่าว)", MsgBoxStyle.OkCancel)
            End If

            If editStat = 1 Then
                connect = CONDB.NewConnection()

                Dim dtp As DataTable = connect.GetTable("SELECT PADD, PDEL, PUPD, PPRT, PSEE, STATUS FROM usrpolicy WHERE UPID = '" & CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("UPID").Value() & "'")
                Dim padd As Boolean = dtp.Rows(0)("PADD")
                Dim pdel As Boolean = dtp.Rows(0)("PDEL")
                Dim pupd As Boolean = dtp.Rows(0)("PUPD")
                Dim pprt As Boolean = dtp.Rows(0)("PPRT")
                Dim psee As Boolean = dtp.Rows(0)("PSEE")
                Dim status As Boolean = dtp.Rows(0)("STATUS")

                Dim Sql As String
                Sql = ""

                If e.GridCell.ColumnIndex = 5 Then
                    If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ADD").Value() = True Then
                        Sql += "UPDATE usrpolicy SET PADD = '1'"
                    Else
                        If pdel = False And pupd = False And pprt = False And psee = False Then
                            Sql += "DELETE FROM usrpolicy"
                        Else
                            Sql += "UPDATE usrpolicy SET PADD = '0'"
                        End If
                    End If
                ElseIf e.GridCell.ColumnIndex = 6 Then
                    If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("DEL").Value() = True Then
                        Sql += "UPDATE usrpolicy SET PDEL = '1'"
                    Else
                        If padd = False And pupd = False And pprt = False And psee = False Then
                            Sql += "DELETE FROM usrpolicy"
                        Else
                            Sql += "UPDATE usrpolicy SET PDEL = '0'"
                        End If
                    End If
                ElseIf e.GridCell.ColumnIndex = 7 Then
                    If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("UPDATE").Value() = True Then
                        Sql += "UPDATE usrpolicy SET PUPD = '1'"
                    Else
                        If padd = False And pdel = False And pprt = False And psee = False Then
                            Sql += "DELETE FROM usrpolicy"
                        Else
                            Sql += "UPDATE usrpolicy SET PUPD = '0'"
                        End If
                    End If
                ElseIf e.GridCell.ColumnIndex = 8 Then
                    If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("PRINT").Value() = True Then
                        Sql += "UPDATE usrpolicy SET PPRT = '1'"
                    Else
                        If padd = False And pdel = False And pupd = False And psee = False Then
                            Sql += "DELETE FROM usrpolicy"
                        Else
                            Sql += "UPDATE usrpolicy SET PPRT = '0'"
                        End If
                    End If
                ElseIf e.GridCell.ColumnIndex = 9 Then
                    If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("VIEW").Value() = True Then
                        Sql += "UPDATE usrpolicy SET PSEE = '1'"
                    Else
                        If padd = False And pdel = False And pupd = False And pprt = False Then
                            Sql += "DELETE FROM usrpolicy"
                        Else
                            Sql += "UPDATE usrpolicy SET PSEE = '0'"
                        End If
                    End If
                ElseIf e.GridCell.ColumnIndex = 10 Then
                    If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = False Then
                        Sql += "DELETE FROM usrpolicy"
                    End If
                End If

                Sql += " WHERE UPID = '"
                Sql += CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("UPID").Value().ToString
                Sql += "'"
                'MsgBox(Sql)

                Try
                    connect.BeginTrans()
                    connect.ExecuteNonQuery(Sql)
                    connect.CommitTrans()
                    queryUserPolicie()
                Catch ex As Exception
                    connect.RollbackTrans()
                    MsgBox(ex.ToString)
                End Try
            Else
                queryUserPolicie()
            End If
        End If
    End Sub

    Private Sub grantPermissionBtn_Click(sender As Object, e As EventArgs) Handles grantPermissionBtn.Click
        If prgListTxt.Text.ToString.Length > 0 Then
            Dim objAddProgramAndPermission As New USERGROUPCLASS
            objAddProgramAndPermission.EMPID_ = empIDLa.Text
            objAddProgramAndPermission.PRGID_ = prgListTxt.Tag
            objAddProgramAndPermission.PADD_ = addChk.Checked
            objAddProgramAndPermission.PDEL_ = delChk.Checked
            objAddProgramAndPermission.PUPD_ = updateChk.Checked
            objAddProgramAndPermission.PPRT_ = printChk.Checked
            objAddProgramAndPermission.PSEE_ = viewChk.Checked
            objAddProgramAndPermission.STATUS_ = statusChk.Checked

            If objAddProgramAndPermission.addUserPolicy() = True Then
                MsgBox("ท่านได้เพิ่มสิทธิ์การใช้โปรแกรมดังกล่าวให้แก่ผู้ใช้รายนี้แล้ว")
                queryUserPolicie()
                filterSoftware()
                clearAddPolicieForm()
            End If

            connect.Dispose()
        Else
            MsgBox("ท่านต้องทำการกรอกข้อมูลให้ครบถ้วนทุกช่องก่อนทำการเพิ่มข้อมูล")
        End If
    End Sub

    
    Private Sub grantGroupPermission_Click(sender As Object, e As EventArgs) Handles grantGroupPermission.Click
        If prgListTxt.Text.Length.ToString > 0 Then
            Dim editStat As Integer

            editStat = MsgBox("ท่านต้องการแก้ไขสิทธิ์การใช้งานซอฟต์แวร์ดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)

            If editStat = 1 Then
                Dim objSetPermissionByGroupSoftware As New SOFTWAREGROUPCLASS
                objSetPermissionByGroupSoftware.GPRGID_ = prgListTxt.Tag
                objSetPermissionByGroupSoftware.EMPID_ = empIDLa.Text

                If objSetPermissionByGroupSoftware.setPermissionByGroupprg = True Then
                    MsgBox("ท่านได้ทำการแก้ไขสิทธิ์ของผู้ใช้เรียบร้อยแล้ว : กลุ่มผู้ใช้ = " & prgListTxt.Text)
                    queryUserPolicie()
                    filterGroupPolicy()
                    clearAddPolicieForm()
                End If
            End If
        End If
    End Sub
End Class